package com.paomi.client.activity.takeTicket.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.takeTicket.model.TakeTicketBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by pc on 2018/3/13.
 * 取票人信息的列表适配
 */

public class TakeListAdapter extends RecyclerView.Adapter<TakeListAdapter.ViewHolder> {
    Context context;
    List<TakeTicketBean> mlist=new ArrayList<>();
    TakeListAdapter.OnItemClick onClickListener;
    private Typeface mType;

    public void OnItemClick(TakeListAdapter.OnItemClick onClickListener) {
        this.onClickListener = onClickListener;
    }


    public TakeListAdapter(Context context, Typeface typeface) {
        this.context = context;
        this.mType = typeface;
    }
    public void setData( List<TakeTicketBean> list){
        this.mlist = list;
    }

    @Override
    public TakeListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new TakeListAdapter.ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_take_ticket, parent, false));
    }

    @Override
    public void onBindViewHolder(final TakeListAdapter.ViewHolder holder, final int position) {
        if(position==0){
            holder.line_add.setVisibility(View.VISIBLE);
            holder.show_txt.setVisibility(View.VISIBLE);
        }else{
            holder.line_add.setVisibility(View.GONE);
            holder.show_txt.setVisibility(View.GONE);
        }
        TakeTicketBean ticket=mlist.get(position);
        if(ticket.getType()==0){
            holder.tv_default.setText(context.getResources().getString(R.string.uncheck_sex));
            holder.tv_default.setTextColor(context.getResources().getColor(R.color.text_gray_99));
        }else if(ticket.getType()==1){
            holder.tv_default.setText(context.getResources().getString(R.string.check_sex));
            holder.tv_default.setTextColor(context.getResources().getColor(R.color.main_color));
        }
        holder.name_tv.setText(ticket.getName());
        holder.mobile_tv.setText(ticket.getMobile());
        holder.mobile_tv.setText(ticket.getMobile());
        holder.tv_default.setTypeface(mType);
        holder.tv_default.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onClickListener!=null){
                    for (int i=0;i<mlist.size();i++){
                        if (i==position){
                            mlist.get(i).setType(1);
                        }else{
                            mlist.get(i).setType(0);
                        }
                    }
                    notifyDataSetChanged();
                    onClickListener.setOnCheckClick(holder.tv_default,position);
                }
            }
        });
        holder.edit_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onClickListener!=null){
                    onClickListener.setOnItemClick(holder.edit_tv,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.name_tv)
        TextView name_tv;
        @BindView(R.id.mobile_tv)
        TextView mobile_tv;
        @BindView(R.id.show_txt)
        TextView show_txt;
        @BindView(R.id.edit_tv)
        TextView edit_tv;
        @BindView(R.id.tv_default)
        TextView tv_default;
        @BindView(R.id.line_add)
        View line_add;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public interface OnItemClick {
        void setOnItemClick(View view, int pos);
        void setOnCheckClick(View view, int pos);
    }
}
